package com.decent.ejfadmin.product.controller;

import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.Page;
import com.decent.ejfadmin.common.bean.SupUser;
import com.decent.ejfadmin.common.bean.SupplyModel;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.product.request.*;
import com.decent.ejfadmin.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchao
 * @date 2018/7/26 9:27
 */
@Slf4j
@RestController
@RequestMapping("productPrice")
public class ProductPriceController {

    @Autowired
    private ProductService productService;

    /**
     * 获取会员模板密价
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "findModelPricePage", method = RequestMethod.GET)
    public Page findModelPricePage(ModelPriceParam param) {
        Page page = new Page();
        try {
            page = productService.findModelPricePage(param);
        } catch (Exception e) {
            page.setTotal(0);
            log.warn("ProductPriceController findModelPricePage occurs error Internal Server Error  ");
        }
        return page;
    }

    /**
     * 设置会员模板密价
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "setUpModelPrice", method = RequestMethod.POST)
    public MessageBean setUpModelPrice(SetUpModelPriceParam param) {
        MessageBean messageBean = MessageBean.success();
        try {
            productService.setUpModelPrice(param);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductPriceController setUpModelPrice occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductPriceController setUpModelPrice occurs error Internal Server Error  " + e.getMessage());
        }
        return messageBean;
    }

    /**
     * 获取会员密价
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "findUserPricePage", method = RequestMethod.GET)
    public Page findUserPricePage(UserPriceParam param) {
        Page page = new Page();
        try {
            page = productService.findUserPricePage(param);
        } catch (Exception e) {
            page.setTotal(0);
            log.warn("ProductPriceController findUserPricePage occurs error Internal Server Error {}", e, e);
        }
        return page;
    }

    /**
     * 设置会员密价
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "setUpUserPrice", method = RequestMethod.POST)
    public MessageBean setUpUserPrice(SetUpUserPriceParam param) {
        MessageBean messageBean = MessageBean.success();
        try {
            productService.setUpUserPrice(param);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductPriceController setUpUserPrice occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductPriceController setUpUserPrice occurs error Internal Server Error  ");
        }
        return messageBean;
    }

    /**
     * 添加密价模板
     *
     * @param supplyModel
     * @return
     */
    @RequestMapping(value = "addPriceModel", method = RequestMethod.POST)
    public MessageBean addPriceModel(SupplyModel supplyModel) {
        MessageBean messageBean = MessageBean.success();
        try {
            if (StringUtils.isBlank(supplyModel.getModelName())) {
                return MessageBean.fail("模板名称不能为空");
            }
            productService.addPriceModel(supplyModel);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductPriceController addPriceModel occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductPriceController addPriceModel occurs error Internal Server Error  ", e);
        }
        return messageBean;
    }

    /**
     * 修改密价模板
     *
     * @param supplyModel
     * @return
     */
    @RequestMapping(value = "modifyPriceModel", method = RequestMethod.POST)
    public MessageBean modifyPriceModel(SupplyModel supplyModel) {
        MessageBean messageBean = MessageBean.success();
        try {
            if (StringUtils.isBlank(supplyModel.getModelName())) {
                return MessageBean.fail("模板名称不能为空");
            }
            productService.modifyPriceModel(supplyModel);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductPriceController modifyPriceModel occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductPriceController modifyPriceModel occurs error Internal Server Error  ", e);
        }
        return messageBean;
    }


    /**
     * 设置会员推荐返利折扣
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "setUpRebateDiscount", method = RequestMethod.POST)
    public MessageBean setUpRebateDiscount(SetUpRebateDiscountParam param) {
        MessageBean messageBean = MessageBean.success();
        try {
            productService.setUpRebateDiscount(param);
        } catch (SupException e) {
            messageBean = MessageBean.fail(e.getResMessage());
            log.warn("ProductPriceController setUpRebateDiscount occurs error Internal Server Error  " + e.getResMessage());
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductPriceController setUpRebateDiscount occurs error Internal Server Error  ", e);
        }
        return messageBean;
    }

    /**
     * 获取所有会员密价模板
     *
     * @param param 是否查询超快销和慢销 true ：是
     * @return
     */
    @RequestMapping(value = "findModelPrice")
    public List findModelPrice(boolean param) {
        try {
            return productService.findModelPrice(param);
        } catch (Exception e) {
            log.warn("ProductPriceController findModelPrice occurs error Internal Server Error  ", e);
            return Collections.emptyList();
        }
    }

    /**
     * 查询所有会员
     *
     * @param userAccount 会员编号
     * @return
     */
    @RequestMapping(value = "queryUserByAccount")
    public List<SupUser> queryUserByAccount(String userAccount) {
        try {
            return productService.queryUserByAccount(userAccount);
        } catch (Exception e) {
            log.warn("ProductPriceController queryUserByAccount occurs error Internal Server Error  ", e);
            return Collections.emptyList();
        }
    }

    /**
     * 会员密价克隆
     *
     * @param sourceUserAccount
     * @param targetUserAccount
     * @param productType
     * @return
     */
    @RequestMapping(value = "cloneUserPrice", method = RequestMethod.POST)
    public MessageBean cloneUserPrice(String sourceUserAccount, String targetUserAccount, Integer productType) {
        MessageBean messageBean = MessageBean.success();
        try {
            if (StringUtils.isBlank(sourceUserAccount) || StringUtils.isBlank(targetUserAccount)) {
                return MessageBean.fail("请检查输入参数");
            }
            return productService.cloneUserPrice(sourceUserAccount, targetUserAccount, productType);
        } catch (Exception e) {
            messageBean = MessageBean.fail();
            log.warn("ProductPriceController cloneUserPrice occurs error Internal Server Error  ", e);
        }
        return messageBean;
    }

    /**
     * 设置密价状态
     *
     * @param ids               选中行
     * @param secretPriceStatus 会员密价状态：1上架 0下架
     * @return
     */
    @RequestMapping("setCecretPriceStatus")
    public MessageBean setCecretPriceStatus(Long[] ids, Integer secretPriceStatus) {
        try {
            if (Objects.isNull(ids)) {
                return MessageBean.fail("请选中行");
            }
            return productService.setCecretPriceStatus(ids, secretPriceStatus);
        } catch (Exception e) {
            log.warn("ProductPriceController setCecretPriceStatus ERROR {}", e, e);
            return MessageBean.fail();
        }
    }
}
